using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Prologis.ShifterNT.Datasource
{


    public class LargeTextfileReader : IEnumerable<string>, IDisposable
    {

        StreamReader sr;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:LargeTextfileReader"/> class.
        /// </summary>
        /// <param name="path">The path.</param>
        public LargeTextfileReader(string path, Encoding encoding)
        {
            sr = new StreamReader(path, encoding);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (sr != null) sr.Close();
        }


        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<string> GetEnumerator()
        {
            while (sr.Peek() != -1)
            {
                yield return sr.ReadLine();
            }
            Dispose();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

}
